<?
 require_once("functions.php");
 require_once("users.php");
 require_once("address.php");

function getAddresses($Customer_id)
{
	$database = getDB();
	
	$statement = "SELECT Address.id AS id, address0, address1, city, state, country, postal_code, value AS Contact_type FROM Address LEFT JOIN Keyword ON Contact_type_id = Keyword.id WHERE Customer_Name_id1 = '$Customer_id'";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}

function updateAddress($updatearray)
{
	$database = getDB();
	
	$updatearray = arrayEscape($updatearray, $database);
	
	$statement = "UPDATE Address SET address0 = '$updatearray[address0]', address1 = '$updatearray[address1]', city = '$updatearray[city]', state = '$updatearray[state]', country = '$updatearray[country]', postal_code = '$updatearray[postal_code]', Contact_type_id = '$updatearray[Contact_type_id]' WHERE id = '$updatearray[id]'";

	$results = $database->query($statement);
	
	$error = $database->error;
	
	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);	
}

function getAddressInfo($id)
{
	$database = getDB();

	$statement = "SELECT Address.id AS id, address0, address1, city, state, country, postal_code, Contact_type_id, value AS Contact_type FROM Address LEFT JOIN Keyword ON Contact_type_id = Keyword.id WHERE Address.id = $id";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}

function addAddress($new_address_array)
{
	$database = getDB();
	
	$new_address_array = arrayEscape($new_address_array, $database);
	
	$statement = "INSERT INTO Address (address0, address1, city, state, country, postal_code, Customer_Name_id1, Contact_type_id) VALUES ('$new_address_array[address0]', '$new_address_array[address1]', '$new_address_array[city]', '$new_address_array[state]', '$new_address_array[country]', '$new_address_array[postal_code]', '$new_address_array[Customer_Name_id1]', '$new_address_array[Contact_type_id]' )";

	$results = $database->query($statement);
	
	$error = $database->error;

	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);
	
}

function delAddress($id)
{
	$database = getDB();
	
	$statement = "DELETE FROM Address WHERE id = '$id'";

	$results = $database->query($statement);

	$error = $database->error;

	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);
	
}

?>